﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using GlobalHelper;

namespace Loader
{
    /// <summary>
    /// 清理时刻表明细数据
    /// 约束: 输入的明细表中,不应包含标题及站名行
    /// </summary>
    public class PreOperator
    {
        /// <summary>
        /// 显示时行数
        /// 用于显示时构造DataTable
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static int GetShowRowAmount(DataTable dt)
        {
            if (Helper.IsEmpty(dt))
            {
                return 0;
            }

            int result = -1;

            result = dt.Rows.Count;

            return result;
        }

        /// <summary>
        /// 显示时列数
        /// 用于显示时构造DataTable
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static int GetShowColAmount(DataTable dt)
        {
            if (Helper.IsEmpty(dt))
            {
                return 0;
            }

            int result = -1;

            result = dt.Columns.Count;

            return result;
        }


        /// <summary>
        /// 对明细中所有的路牌名,加<>标记
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DataTable SetTokenName(DataTable dt)
        {
            if (Helper.IsEmpty(dt))
            {
                return dt;
            }

            DataTable result = dt;

            //1. 将首列中的所有非空项,收集为路牌名集合
            IList<string> tokenNames = GetTokenNames(result);

            //2. 根据路牌名集合,在明细中寻找所有匹配处
            for (int i = 0; i < result.Rows.Count; i++)
            {
                for (int j = 0; j < result.Columns.Count; j++)
                {
                    foreach (string tokenName in tokenNames)
                    {
                        if (IsSameToken(result.Rows[i][j].ToString(), tokenName))
                        {
                            //3. 修改匹配处, 加<>
                            result.Rows[i][j] = string.Format("<{0}>", result.Rows[i][j]);
                        }
                    }

                }
            }

            return result;
        }

        private static bool IsSameToken(string detail, string tokenName)
        {
            bool result = false;

            //1. 如果是 '< '开始的, '> ' 结束的, detail, 去除<>后与tokenName比较
            if (detail.StartsWith(Helper.TokenBeginFlag)
                && detail.EndsWith(Helper.TokenEndFlag))
            {
                string temp = detail;
                temp = temp.Replace(Helper.TokenBeginFlag, "");
                temp = temp.Replace(Helper.TokenEndFlag, "");

                //result = temp == tokenName;
            }
            //2. 否则,直接比较名称
            else
            {
                result = detail == tokenName;
            }


            return result;
        }

        /// <summary>
        /// 删除明细中各种状态的标识,如:吃饭,接落等
        /// 仅为集团时刻表导入之用
        /// 
        /// 约束: 路牌名应事先规格化
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DataTable RemoveFlag(DataTable dt)
        {
            if (Helper.IsEmpty(dt))
            {
                return dt;
            }

            DataTable result = dt;

            //1. 遍历所有明细
            for (int i = 0; i < result.Rows.Count; i++)
            {
                for (int j = 0; j < result.Columns.Count; j++)
                {
                    string content = result.Rows[i][j].ToString();

                    //2. 判断 含非数字
                    if (Helper.CheckIsNumber(content))
                    {
                        continue;
                    }

                    //3. 跳过,所有 路牌名,及 结束
                    if (ISTokenName(content))
                    {
                        continue;
                    }
                    if (ISEnd(content))
                    {
                        continue;
                    }

                    //4. 去除非数字符号
                    result.Rows[i][j] = RemoveContentFlag(content);
                }
            }




            return result;
        }



        /// <summary>
        /// 统计路牌数
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static int GetTokenAmount(DataTable dt)
        {
            if (Helper.IsEmpty(dt))
            {
                return 0;
            }

            int result = -1;

            //统计首列, 非空元个数
            result = GetTokenNames(dt).Count;

            return result;
        }

        /// <summary>
        /// 统计周转数
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static int GetTurnAmount(DataTable dt)
        {
            if (Helper.IsEmpty(dt))
            {
                return 0;
            }

            int result = -1;

            //1. 统计行数
            //2. 行数统计结果 -2 (路牌名列 与 最后结束列)
            result = dt.Columns.Count - 2;

            return result;
        }

        #region Function

        /// <summary>
        /// 获得明细中所有路牌名称
        /// 依据首列
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private static IList<string> GetTokenNames(DataTable dt)
        {
            IList<string> result = new List<string>();

            foreach (DataRow dataRow in dt.Rows)
            {
                if (!string.IsNullOrEmpty(dataRow.ItemArray[0].ToString()))
                {
                    result.Add(dataRow.ItemArray[0].ToString());
                }
            }

            return result;
        }

        /// <summary>
        /// 移除 字符串中所有非数字
        /// 保留数字
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        private static string RemoveContentFlag(string content)
        {
            string result = content;

            string s = Regex.Replace(content, @"\D", "");

            if (!string.IsNullOrEmpty(s))
            {
                result = s;
            }

            return result;
        }


        /// <summary>
        /// 判断字符串为路牌结束
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        private static bool ISEnd(string content)
        {
            bool result = false;

            IList<string> endFlag = new string[] { "※", "x", "X", };

            foreach (string s in endFlag)
            {
                if (content == s)
                {
                    result = true;
                    break;
                }
            }

            return result;
        }

        /// <summary>
        /// 判断 字符串 为路牌名
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        private static bool ISTokenName(string content)
        {
            bool result = false;

            if (content.StartsWith(Helper.TokenBeginFlag)
                && content.EndsWith(Helper.TokenEndFlag))
            {
                result = true;
            }

            return result;
        }

        #endregion

        /// <summary>
        /// 统一修正表格中符号
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DataTable FixFlag(DataTable dt)
        {
            if (Helper.IsEmpty(dt))
            {
                return null;
            }

            DataTable result = dt;

            #region 当路牌结束符号单独占用一个单元格时,将其合并至前一单元格,并清空原来占用的单元格

            for (int i = 0; i < result.Rows.Count; i++)
            {
                for (int j = 0; j < result.Columns.Count; j++)
                {
                    if (!string.IsNullOrEmpty(result.Rows[i][j].ToString()) 
                        && Helper.IsInList(Helper.TokenLineEndFlag,result.Rows[i][j].ToString()))
                    {
                        if (string.IsNullOrEmpty(result.Rows[i][j-1].ToString()))
                        {
                            throw new Exception("输入的数据格式不正确,路牌结束符号之前为空!");
                        }

                        if (!Helper.CheckIsNumber(result.Rows[i][j-1].ToString()))
                        {
                            throw new Exception("输入的数据格式不正确,路牌结束符号之前不是数字!");
                        }

                        result.Rows[i][j - 1] += result.Rows[i][j].ToString();
                        result.Rows[i][j] = string.Empty;
                    }
                }
            }

            #endregion

            return result;
        }
    }
}
